package activities;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import com.matchcalendar.R;

import algorithm.CalendarAlgorithm;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TimePicker;

public class SetMatchTimeRangeActivity extends Activity {
	private Button buttonPrevious, buttonDone;
	private ImageView imageViewSunday, imageViewMonday, imageViewTuesday, imageViewWednesday,
	imageViewThursday, imageViewFriday, imageViewSaturday, imageViewStartTime, imageViewOverTime;
	private Set<Integer> selectDaysSet = new HashSet<Integer>();
	private TextView textViewStartTime, textViewOverTime;
	private Calendar startTimeCal = Calendar.getInstance();
	private Calendar overTimeCal = Calendar.getInstance();
	private boolean haveChooseOverTime = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_set_match_time_range);
		findView();
		setListenerOrAdapter();
		initView();
	}

	private void findView() {
		buttonPrevious = (Button) findViewById(R.id.buttonPrevious);
		imageViewSunday = (ImageView) findViewById(R.id.imageViewSunday);
		imageViewMonday = (ImageView) findViewById(R.id.imageViewMonday);
		imageViewTuesday = (ImageView) findViewById(R.id.imageViewTuesday);
		imageViewWednesday = (ImageView) findViewById(R.id.imageViewWednesday);
		imageViewThursday = (ImageView) findViewById(R.id.imageViewThursday);
		imageViewFriday = (ImageView) findViewById(R.id.imageViewFriday);
		imageViewSaturday = (ImageView) findViewById(R.id.imageViewSaturday);
		textViewStartTime = (TextView) findViewById(R.id.textViewStartTime);
		imageViewStartTime = (ImageView) findViewById(R.id.imageViewStartTime);
		imageViewOverTime = (ImageView) findViewById(R.id.imageViewOverTime);
		textViewOverTime = (TextView) findViewById(R.id.textViewOverTime);
		buttonDone = (Button) findViewById(R.id.buttonDone);
	}
	
	private void setListenerOrAdapter() {
		buttonPrevious.setOnClickListener(new ButtonPreviousClick());
		imageViewSunday.setOnClickListener(new ImageViewDaysClick());
		imageViewMonday.setOnClickListener(new ImageViewDaysClick());
		imageViewTuesday.setOnClickListener(new ImageViewDaysClick());
		imageViewWednesday.setOnClickListener(new ImageViewDaysClick());
		imageViewThursday.setOnClickListener(new ImageViewDaysClick());
		imageViewFriday.setOnClickListener(new ImageViewDaysClick());
		imageViewSaturday.setOnClickListener(new ImageViewDaysClick());
		imageViewStartTime.setOnClickListener(new ImageViewStartTimeClick());
		imageViewOverTime.setOnClickListener(new ImageViewOverTimeClick());
		buttonDone.setOnClickListener(new ButtonDoneClick());
	}
	
	private class ButtonPreviousClick implements Button.OnClickListener {

		@Override
		public void onClick(View arg0) {
			SetMatchTimeRangeActivity.this.finish();
		}
	}
	
	private class ImageViewDaysClick implements ImageView.OnClickListener {

		@Override
		public void onClick(View view) {
			int theDay = 0;
			int id = view.getId();
			
			if (id == R.id.imageViewSunday)
				theDay = 0;
			else if (id == R.id.imageViewMonday)
				theDay = 1;
			else if (id == R.id.imageViewTuesday)
				theDay = 2;
			else if (id == R.id.imageViewWednesday)
				theDay = 3;
			else if (id == R.id.imageViewThursday)
				theDay = 4;
			else if (id == R.id.imageViewFriday)
				theDay = 5;
			else if (id == R.id.imageViewSaturday)
				theDay = 6;
			if (selectDaysSet.contains(theDay)) {
				selectDaysSet.remove(theDay);
				((ImageView)view).setImageResource(R.drawable.schedule_shallow);
			}
			else {
				selectDaysSet.add(theDay);
				((ImageView)view).setImageResource(R.drawable.schedule_deep);
			}
		}
	}
	
	private class ImageViewStartTimeClick implements ImageView.OnClickListener {
		
		@Override
		public void onClick(View v) {
			TimePicker startTimePicker = new TimePicker(SetMatchTimeRangeActivity.this);
			
			startTimePicker.setIs24HourView(true);
			startTimePicker.setCurrentHour(startTimeCal.get(Calendar.HOUR_OF_DAY));
			startTimePicker.setCurrentMinute(startTimeCal.get(Calendar.MINUTE));
			startTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
			new AlertDialog.Builder(SetMatchTimeRangeActivity.this)
			.setTitle("選擇開始時間")
			.setView(startTimePicker)
			.setNegativeButton("取消", null)
			.setPositiveButton("確定", new StartTimeDialogPositiveClick(startTimePicker))
			.show();
		}
		
		private class StartTimeDialogPositiveClick implements DialogInterface.OnClickListener {
			private TimePicker startTimePicker;

			public StartTimeDialogPositiveClick(TimePicker startTimePicker) {
				this.startTimePicker = startTimePicker;
			}

			@Override
			public void onClick(DialogInterface arg0, int arg1) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
				
				startTimeCal.set(Calendar.HOUR_OF_DAY, startTimePicker.getCurrentHour());
				startTimeCal.set(Calendar.MINUTE, startTimePicker.getCurrentMinute());
				textViewStartTime.setText(sdf.format(startTimeCal.getTime()));
			}
		}
	}
	
	private class ImageViewOverTimeClick implements ImageView.OnClickListener {

		@Override
		public void onClick(View v) {
			TimePicker overTimePicker = new TimePicker(SetMatchTimeRangeActivity.this);
			
			overTimePicker.setIs24HourView(true);
			overTimePicker.setCurrentHour(overTimeCal.get(Calendar.HOUR_OF_DAY));
			overTimePicker.setCurrentMinute(overTimeCal.get(Calendar.MINUTE));
			overTimePicker.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
			new AlertDialog.Builder(SetMatchTimeRangeActivity.this)
			.setTitle("選擇結束時間")
			.setView(overTimePicker)
			.setNegativeButton("取消", null)
			.setPositiveButton("確定", new OverTimeDialogPositiveClick(overTimePicker))
			.show();
		}
		
		private class OverTimeDialogPositiveClick implements DialogInterface.OnClickListener {
			private TimePicker overTimePicker;

			public OverTimeDialogPositiveClick(TimePicker overTimePicker) {
				this.overTimePicker = overTimePicker;
			}

			@Override
			public void onClick(DialogInterface dialog, int which) {
				SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
				
				overTimeCal.set(Calendar.HOUR_OF_DAY, overTimePicker.getCurrentHour());
				overTimeCal.set(Calendar.MINUTE, overTimePicker.getCurrentMinute());
				textViewOverTime.setText(sdf.format(overTimeCal.getTime()));
				haveChooseOverTime = true;
			}
		}
	}
	
	private class ButtonDoneClick implements Button.OnClickListener {

		@Override
		public void onClick(View v) {
			if (selectDaysSet.isEmpty()) {
				new AlertDialog.Builder(SetMatchTimeRangeActivity.this)
				.setTitle("請選擇天")
				.setMessage("請選擇您要哪幾天套用此活動時間")
				.setPositiveButton("好", null)
				.show();
			}
			else if (!haveChooseOverTime) {
				new AlertDialog.Builder(SetMatchTimeRangeActivity.this)
				.setTitle("尚未選擇活動結束時間")
				.setMessage("請選擇活動結束時間")
				.setPositiveButton("好", null)
				.show();
			}
			else if (CalendarAlgorithm.compareTime(startTimeCal, overTimeCal) >= 0) {
				new AlertDialog.Builder(SetMatchTimeRangeActivity.this)
				.setTitle("活動時間範圍不正確")
				.setMessage("活動開始時間須小於活動結束時間")
				.setPositiveButton("好", null)
				.show();
			}
			else {
				Bundle bundle = new Bundle();
				Intent intent = new Intent();
				ArrayList<Integer> selectDaysList = new ArrayList<Integer>();
				
				for (int day: selectDaysSet)
					selectDaysList.add(day);
				bundle.putIntegerArrayList("selectDaysList", selectDaysList);
				bundle.putInt("startHour", startTimeCal.get(Calendar.HOUR_OF_DAY));
				bundle.putInt("startMinute", startTimeCal.get(Calendar.MINUTE));
				bundle.putInt("overHour", overTimeCal.get(Calendar.HOUR_OF_DAY));
				bundle.putInt("overMinute", overTimeCal.get(Calendar.MINUTE));
				intent.putExtras(bundle);
				setResult(RESULT_OK, intent);
				SetMatchTimeRangeActivity.this.finish();
			}
		}
	}
	
	private void initView() {
		Bundle bundle = getIntent().getExtras();
		SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
		
		selectDaysSet.add(bundle.getInt("day"));
		switch (bundle.getInt("day")) {
			case 0:
				imageViewSunday.setImageResource(R.drawable.schedule_deep);
				break;
			case 1:
				imageViewMonday.setImageResource(R.drawable.schedule_deep);
				break;
			case 2:
				imageViewTuesday.setImageResource(R.drawable.schedule_deep);
				break;
			case 3:
				imageViewWednesday.setImageResource(R.drawable.schedule_deep);
				break;
			case 4:
				imageViewThursday.setImageResource(R.drawable.schedule_deep);
				break;
			case 5:
				imageViewFriday.setImageResource(R.drawable.schedule_deep);
				break;
			case 6:
				imageViewSaturday.setImageResource(R.drawable.schedule_deep);
		}
		startTimeCal.set(Calendar.HOUR_OF_DAY, bundle.getInt("hour"));
		startTimeCal.set(Calendar.MINUTE, 0);
		textViewStartTime.setText(sdf.format(startTimeCal.getTime()));
	}
}
